<?php $this->headScript()
->appendFile($this->js('plugins/jquery.form.js'))
->appendFile($this->js('plugins/jquery.blockUI.js'))
?>
<script>
$(function(){
    $("#yearSemester").change(function(){
        $("#selectForm").submit();
    });
    $("#select-grade").change(function(){
        $("#selectForm").submit();
     });
    $("#select-subject,#stage_id").change(function(){
    	$("#selectForm").submit();
     });
    $("#scoreForm").ajaxForm({
        success: function(){
        checkFail();
        }
        });

    $("#saveBtn").click(function(){
        if ($("#stage_id").val()=='') {
            alert('<?php echo $this->translate("state cant empty")?>');
            return false;
        }
     //   $("#selectForm").attr('action','<?php echo $this->route("set")?>');
        $("#act").val('setData');
        $.blockUI({ message: '<h1><img src="<?php echo $this->BaseUrl()?>/images/loading.gif" /> <?php echo $this->translate("Just a moment..")?>.</h1>' });
       $("#selectForm").submit();
     });

     $(".score").focus(function(){
         $(".score").removeClass('focus');
         $(this).addClass('focus');
         var x = $(this).offset().top - 100; // 100 provides buffer in viewport
         $('html,body').animate({scrollTop: x}, 50);
        });

    // 輸入處理
 $(".score").keydown(function(e){
   var tabIndex =  parseInt($(this).attr('tabindex'));

     switch (e.which) {
   case 9 : //tab
       var score = parseFloat($(this).attr('value'));
       if (isNaN(score)) score = -1;
       if (score < 0 || score > 100) {
           $(this).attr('value','');
           tabIndex --;
       }
       break;
   case 13 : // return
       var score = parseFloat($(this).attr('value'));
       if (isNaN(score)) score = -1;
       if (score < 0 || score > 100) {
           alert($(this).attr('value')+' <?php echo $this->translate('value error')?>');
           $(this).attr('value','');
       }
       else
           tabIndex++;
       break;
   case 40 :
       tabIndex ++;
    break;
   case 38 :
       tabIndex --;
    break;
   }
   if ($(".score[tabindex="+tabIndex+"]").length) {
       $(".score[tabindex="+tabIndex+"]").focus();
   }
//   else
//       $("#signBtn").focus();
});


    $("#score-list table tbody tr:even").addClass('even');
    $("#score-list table tbody tr:odd").addClass('odd');
    // 檢查不及格
    checkFail();
});

function checkFail()
{
    $.each($("#score-list .score"),function(){
        $(this).removeClass('failed');
        var s = $(this).attr('value');
        if (s !='' && s < 60)
              $(this).addClass('failed');
     });
}
</script>
<style>
#score-list table {margin:5px; padding:5px; text-align:center; width:450px;
background: #fed; border-spacing: 1px;}
.failed {color:red}
.focus {background:#ff0}
</style>

<div class="ui-widget">
<form id="selectForm" action="" method="post">
<select name="yearSemester"  id="yearSemester">
<?php echo $this->YearSemesterOptions($this->yearSemester,array('order'=>'year DESC'))?>
</select>
<select id="select-grade" name="select-grade">
<option value=""><?php echo $this->translate('select grade ..')?></option>
<?php echo $this->ClassGradeOptions($this->grade)?>
</select>


<select id="select-subject" name="select-subject">
<option value='' ><?php echo $this->translate('select course ..')?></option>
<?php foreach($this->subject as $subjectName=>$val): ?>
<optgroup label="<?php echo $subjectName?>">
<?php foreach($val as $sn=>$groupName):?>
<option value="<?php echo $sn?>" <?php Sfs_Html::printSelected($sn,$this->group_sn)?>><?php echo $groupName?></option>
<?php endforeach;?>
</optgroup>
<?php endforeach;?>
</select>
<select id="stage_id" name="stage_id">
<option value=""><?php echo $this->translate('choose stage ..')?></option>
<?php if(isset($this->scored_mode) and $this->scored_mode == Semester_Model_SemesterSubject::SCORE_MODE_WHOLE_SEMESTER):?>
<option value="<?php echo Score_Model_Statistics::WHOLE_STAGE_SCORE?>"
<?php if (isset($this->stage_id)):?>selected<?php endif?>
 ><?php echo $this->translate('whole stage score')?></option>
<?php else:?>
<?php for($i=1; $i <= $this->period; $i++):?>
<option value="<?php echo $i?>" <?php if($i==$this->stage_id):?>selected<?php endif?> ><?php echo sprintf($this->translate('%dth stage'),$i)?></option>
<?php endfor;?>
<?php endif?>
</select>

<?php if ($this->students):?>
<input type="button"  id="saveBtn" value="<?php echo $this->translate('save score')?>" />
<table>
<thead>
<tr>
<th><?php echo $this->translate('student number')?></th>
<th><?php echo $this->translate('student name')?></th>
<?php if($this->scored_mode == Semester_Model_SemesterSubject::SCORE_MODE_WHOLE_SEMESTER):?>
<th><?php echo $this->translate('whole score')?></th>
<?php else:?>
<th><?php echo $this->translate('regular score')?></th>
<th><?php echo $this->translate('usually score')?></th>
<?php endif?>
</tr>
</thead>
<tbody>
<?php $i=0;?>
<?php foreach($this->students as $row):?>
<tr>
<td><?php echo $row['number']?>  <span style="color:#ccc">[<?php echo $row['class_sn'].'-'.$row['student_number']?>]</span></td>
<td><?php echo $row['name']?></td>
<td>
<?php if($this->scored_mode == Semester_Model_SemesterSubject::SCORE_MODE_WHOLE_SEMESTER):?>
<input type="text"  name="scores[3][<?php echo $row['class_sn']?>-<?php echo $row['student_sn']?>]" size="5" class="score"
tabindex="<?php echo 200+$i++?>"
<?php if (isset($this->scores[$row['student_sn']][3])):?>
value="<?php echo $this->scores[$row['student_sn']][3]?>"
<?php endif?>
/>

<?php else:?>
<input type="text"  name="scores[1][<?php echo $row['class_sn']?>-<?php echo $row['student_sn']?>]"
size="5"
class="score"
tabindex="<?php echo 200+$i?>"
<?php if (isset($this->scores[$row['student_sn']][1])):?>
value="<?php echo $this->scores[$row['student_sn']][1]?>"
<?php endif?>
/>
</td>
<td>
<input type="text"  name="scores[2][<?php echo $row['class_sn']?>-<?php echo $row['student_sn']?>]"
size="5"
class="score"
tabindex="<?php echo 300+$i++?>"
<?php if (isset($this->scores[$row['student_sn']][2])):?>
value="<?php echo $this->scores[$row['student_sn']][2]?>"
<?php endif?>
/>

<?php endif?>
</td>
</tr>
<?php ?>
<?php endforeach;?>
</tbody>
</table>
<?php endif;?>
<input type="hidden"  id="act" name="act"  value="" />
</form>
</div>